package com.evil.application.pojo.dto.form.control.code;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.evil.application.pojo.dto.form.control.code.config.FilterConfigDTO;
import com.evil.application.pojo.dto.form.control.code.config.RegularDTO;
import com.evil.application.pojo.entity.ApplicationFormControl;
import com.evil.common.application.enums.form.DefaultValueTypeEnum;
import com.evil.common.core.enums.SwitchEnum;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Range;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * 表单控件实体
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Data
@NoArgsConstructor
public abstract class ControlDTO {

    public static final String REGULARS = "regulars";
    public static final String CHILD_CONTROLS = "childControls";
    public static final String RELATE_FILTER_CONFIG = "relateFilterConfig";
    public static final String[] ignoreProperties = new String[]{REGULARS, CHILD_CONTROLS, RELATE_FILTER_CONFIG};

    /**
     * 控件id
     */
    @NotNull(message = "控件id不能为空")
    private Long controlId;
    /**
     * 表单id
     */
    private Long formId;
    /**
     * 控件类型
     */
    @NotBlank(message = "控件类型不能为空")
    private String controlCode;
    /**
     * 字段名称
     */
    @NotBlank(message = "字段名称不能为空")
    private String controlField;
    /**
     * 控件标题
     */
    private String controlTitle;
    /**
     * 控件提示
     */
    private String controlTip;
    /**
     * 控件大小，默认24
     */
    private Integer controlSize;
    /**
     * 标签宽度
     */
    private Integer controlLabelSize;
    /**
     * 显示标签(0:否, 1:是)
     */
    @NotNull(message = "显示标签参数不能为空")
    @Range(min = 0, max = 1, message = "参数错误")
    private Integer showLabel;
    /**
     * 排序 越小越靠前
     */
    private Integer sort;
    /**
     * 校验表达式
     */
    private List<RegularDTO> regulars;
    /**
     * 是否必填(0:非必填, 1:必填)
     */
    @NotNull(message = "是否必填参数不能为空")
    @Range(min = 0, max = 1, message = "参数错误")
    private Integer required;
    /**
     * 不允许重复(0:允许, 1:不允许)
     */
    @NotNull(message = "不允许重复参数不能为空")
    @Range(min = 0, max = 1, message = "参数错误")
    private Integer noRepeat;
    /**
     * 样式
     */
    private String style;
    /**
     * 控件类型(前端定制)
     */
    private String tagIcon;
    /**
     * 默认内容类型(0:自定义, 1:关联应用数据, 2: 通过公式运算, 3:关联已有表格)
     */
    @NotNull(message = "默认内容类型参数不能为空")
    private Integer defaultValueType;
    /**
     * 默认内容
     */
    private String defaultValue;
    /**
     * 公式
     */
    private String formula;
    /**
     * 描述|简介
     */
    private String description;
    /**
     * 数据类型
     */
    private String layout;
    /**
     * 强制更新key
     */
    private String renderKey;
    /**
     * 是否可以清除(0:否, 1:是)
     */
    private Integer clearable;
    /**
     * 是否可以多选(0:否, 1:是)
     */
    private Integer multiple;

    /**
     * 关联表单id
     */
    private Long relatedFormId;
    /**
     * 关联表单控件id
     */
    private Long relatedControlId;
    /**
     * 上级控件id
     */
    private Long parentControlId;
    /**
     * 子级控件
     */
    private List<ControlDTO> childControls;

    /**
     * 筛选条件配置
     */
    @Valid
    private FilterConfigDTO relateFilterConfig;

    public ControlDTO(ApplicationFormControl applicationFormControl, String... ignoreProperties) {
        BeanUtil.copyProperties(applicationFormControl, this, ArrayUtils.addAll(ControlDTO.ignoreProperties, ignoreProperties));
        this.childControls = new ArrayList<>();
        this.showLabel = ObjectUtil.defaultIfNull(this.showLabel, SwitchEnum.YES.getId());
        this.required = ObjectUtil.defaultIfNull(this.required, SwitchEnum.NO.getId());
        this.noRepeat = ObjectUtil.defaultIfNull(this.noRepeat, SwitchEnum.NO.getId());
        this.defaultValueType = ObjectUtil.defaultIfNull(this.defaultValueType, DefaultValueTypeEnum.DIY.getId());
        this.regulars = new ArrayList<>();
        if (StringUtils.isNotBlank(applicationFormControl.getRegulars())) {
            this.regulars = JSON.parseArray(applicationFormControl.getRegulars(), RegularDTO.class);
        }
    }
}
